package com.example.cici.tryxutil3_httpandannotation.util;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
import com.example.cici.tryxutil3_httpandannotation.R;

public class CountDownTimerUtils extends CountDownTimer{
    private TextView myTextView;
    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.myTextView = textView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        myTextView.setClickable(false);//设置不可点击
        myTextView.setText(millisUntilFinished / 1000 + "秒后可重新发送");  //设置倒计时时间
        myTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰色，这时是不能点击的
        SpannableString spannableString = new SpannableString(myTextView.getText().toString());  //获取按钮上的文字
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
        myTextView.setText(spannableString);
    }

    @Override
    public void onFinish() {
        myTextView.setText("重新获取验证码");
        myTextView.setClickable(true);
        myTextView.setBackgroundResource(R.drawable.bg_identify_code_normal);
    }
}
